<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Microsoft YaHei:300,300italic,400,400italic,700,700italic|consolas:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Node.js,小项目,mine,Underscore," />








  <link rel="shortcut icon" type="image/x-icon" href="http://ofy7k3v4s.bkt.clouddn.com/mylogo.png?v=5.0.2" />






<meta name="description" content="&amp;#x5B8C;&amp;#x6210;&amp;#x76F8;&amp;#x518C;&amp;#x65B0;&amp;#x5EFA;&amp;#x3001;&amp;#x4E0A;&amp;#x4F20;&amp;#x56FE;&amp;#x7247;&amp;#x7B49;&amp;#x7B80;&amp;#x5355;&amp;#x7684;&amp;#x529F;&amp;#x80FD;&amp;#x5C0F;&amp;#x9879;&amp;#x76EE;&amp;#x3002;
&amp;#x9879;&amp;#x76EE;&amp;#x521D;&amp;#x59CB">
<meta property="og:type" content="article">
<meta property="og:title" content="Node-相册小项目（上）">
<meta property="og:url" content="https://hongqinma.github.io/2016/10/14/s008-node-相册小项目（上）/index.html">
<meta property="og:site_name" content="HongqinMa' Blog">
<meta property="og:description" content="&amp;#x5B8C;&amp;#x6210;&amp;#x76F8;&amp;#x518C;&amp;#x65B0;&amp;#x5EFA;&amp;#x3001;&amp;#x4E0A;&amp;#x4F20;&amp;#x56FE;&amp;#x7247;&amp;#x7B49;&amp;#x7B80;&amp;#x5355;&amp;#x7684;&amp;#x529F;&amp;#x80FD;&amp;#x5C0F;&amp;#x9879;&amp;#x76EE;&amp;#x3002;
&amp;#x9879;&amp;#x76EE;&amp;#x521D;&amp;#x59CB">
<meta property="og:image" content="https://hongqinma.github.io/2016/10/14/s008-node-相册小项目（上）/请求处理模块调用流程图.png">
<meta property="og:updated_time" content="2016-11-30T11:41:22.008Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Node-相册小项目（上）">
<meta name="twitter:description" content="&amp;#x5B8C;&amp;#x6210;&amp;#x76F8;&amp;#x518C;&amp;#x65B0;&amp;#x5EFA;&amp;#x3001;&amp;#x4E0A;&amp;#x4F20;&amp;#x56FE;&amp;#x7247;&amp;#x7B49;&amp;#x7B80;&amp;#x5355;&amp;#x7684;&amp;#x529F;&amp;#x80FD;&amp;#x5C0F;&amp;#x9879;&amp;#x76EE;&amp;#x3002;
&amp;#x9879;&amp;#x76EE;&amp;#x521D;&amp;#x59CB">
<meta name="twitter:image" content="https://hongqinma.github.io/2016/10/14/s008-node-相册小项目（上）/请求处理模块调用流程图.png">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '6347850919329138000',
      author: 'Hiraku'
    }
  };
</script>




  <link rel="canonical" href="https://hongqinma.github.io/2016/10/14/s008-node-相册小项目（上）/"/>


  <title> Node-相册小项目（上） | HongqinMa' Blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?10010323";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta custom-logo">
  
    <div class="site-meta-headline">
      <a>
        <img class="custom-logo-image" src="http://ofy7k3v4s.bkt.clouddn.com/photo.jpg"
             alt="HongqinMa' Blog"/>
      </a>
    </div>
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">HongqinMa' Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">Spell at all costs， to rush you in the future。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Node-相册小项目（上）
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2016-10-14T23:22:12+08:00" content="2016-10-14">
              2016-10-14
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/Node-js/" itemprop="url" rel="index">
                    <span itemprop="name">Node.js</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/10/14/s008-node-相册小项目（上）/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/10/14/s008-node-相册小项目（上）/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          

          
              &nbsp; | &nbsp;
              <span class="page-pv"><i class="fa fa-file-o"></i>
              <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
              </span>
          

          
            <span id="busuanzi_container_page_pv">  <span id="busuanzi_value_page_pv"></span> Views</span>
          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#x5B8C;&#x6210;&#x76F8;&#x518C;&#x65B0;&#x5EFA;&#x3001;&#x4E0A;&#x4F20;&#x56FE;&#x7247;&#x7B49;&#x7B80;&#x5355;&#x7684;&#x529F;&#x80FD;&#x5C0F;&#x9879;&#x76EE;&#x3002;</p>
<h2 id="&#x9879;&#x76EE;&#x521D;&#x59CB;&#x5316;"><a href="#&#x9879;&#x76EE;&#x521D;&#x59CB;&#x5316;" class="headerlink" title="&#x9879;&#x76EE;&#x521D;&#x59CB;&#x5316;"></a>&#x9879;&#x76EE;&#x521D;&#x59CB;&#x5316;</h2><ol>
<li>&#x628A;&#x524D;&#x7AEF;&#x7528;&#x5230;&#x7684;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x653E;&#x5230; <code>public</code> &#x76EE;&#x5F55;&#x4E0B;</li>
<li>&#x628A;&#x6240;&#x6709;&#x7684;&#x9875;&#x9762;&#x90FD;&#x653E;&#x5230; <code>views</code> &#x76EE;&#x5F55;&#x4E0B;</li>
<li>&#x5728;&#x9879;&#x76EE;&#x6839;&#x8DEF;&#x5F84;&#x4E0B;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; <code>app.js</code> &#x4F5C;&#x4E3A;&#x540E;&#x53F0;&#x7684;&#x542F;&#x52A8;&#x5165;&#x53E3;</li>
<li>&#x4F7F;&#x7528; npm &#x5B89;&#x88C5;&#x548C;&#x7BA1;&#x7406;&#x9879;&#x76EE;&#x7684;&#x4F9D;&#x8D56;&#x9879;</li>
<li>&#x5C06;&#x6240;&#x6709;&#x7684;&#x76F8;&#x518C;&#x653E;&#x5230;&#x9879;&#x76EE;&#x6839;&#x8DEF;&#x5F84;&#x4E0B;&#x7684; <code>uploads</code> &#x76EE;&#x5F55;&#x5C0F;</li>
</ol>
<h2 id="&#x5212;&#x5206;&#x54EA;&#x4E9B;&#x8D44;&#x6E90;&#x516C;&#x5171;&#x5F00;&#x653E;"><a href="#&#x5212;&#x5206;&#x54EA;&#x4E9B;&#x8D44;&#x6E90;&#x516C;&#x5171;&#x5F00;&#x653E;" class="headerlink" title="&#x5212;&#x5206;&#x54EA;&#x4E9B;&#x8D44;&#x6E90;&#x516C;&#x5171;&#x5F00;&#x653E;"></a>&#x5212;&#x5206;&#x54EA;&#x4E9B;&#x8D44;&#x6E90;&#x516C;&#x5171;&#x5F00;&#x653E;</h2><ul>
<li>public<ul>
<li>css</li>
<li>js</li>
<li>img</li>
</ul>
</li>
<li>uploads</li>
<li>node_modules</li>
</ul>
<h2 id="&#x8BBE;&#x8BA1;&#x8DEF;&#x7531;"><a href="#&#x8BBE;&#x8BA1;&#x8DEF;&#x7531;" class="headerlink" title="&#x8BBE;&#x8BA1;&#x8DEF;&#x7531;"></a>&#x8BBE;&#x8BA1;&#x8DEF;&#x7531;</h2><table>
<thead>
<tr>
<th>&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;</th>
<th>&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;</th>
<th>&#x54CD;&#x5E94;&#x5904;&#x7406;</th>
</tr>
</thead>
<tbody>
<tr>
<td>GET</td>
<td>/</td>
<td>views/index.html</td>
</tr>
<tr>
<td>GET</td>
<td>/album</td>
<td>views/album.html</td>
</tr>
<tr>
<td>GET</td>
<td>/album/add</td>
<td>&#x5904;&#x7406;&#x6DFB;&#x52A0;&#x76F8;&#x518C;&#x8BF7;&#x6C42;</td>
</tr>
<tr>
<td>POST</td>
<td>/upload</td>
<td>&#x5904;&#x7406;&#x7167;&#x7247;&#x4E0A;&#x4F20;&#x8BF7;&#x6C42;</td>
</tr>
</tbody>
</table>
<h2 id="&#x5904;&#x7406;&#x76F8;&#x518C;&#x9759;&#x6001;&#x8D44;&#x6E90;"><a href="#&#x5904;&#x7406;&#x76F8;&#x518C;&#x9759;&#x6001;&#x8D44;&#x6E90;" class="headerlink" title="&#x5904;&#x7406;&#x76F8;&#x518C;&#x9759;&#x6001;&#x8D44;&#x6E90;"></a>&#x5904;&#x7406;&#x76F8;&#x518C;&#x9759;&#x6001;&#x8D44;&#x6E90;</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">&apos;http&apos;</span>);</div><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="keyword">const</span> url = <span class="built_in">require</span>(<span class="string">&apos;url&apos;</span>);</div><div class="line"><span class="keyword">const</span> mime = <span class="built_in">require</span>(<span class="string">&apos;mime&apos;</span>);</div><div class="line"></div><div class="line">http</div><div class="line">  .createServer()</div><div class="line">  .on(<span class="string">&apos;request&apos;</span>, (req, res) =&gt; {</div><div class="line">    <span class="comment">// &#x6307;&#x5B9A;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;parse &#x65B9;&#x6CD5;&#x4F1A;&#x5C06;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x89E3;&#x6790;&#x4E3A;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x6302;&#x8F7D;&#x7ED9;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x7684; query &#x5C5E;&#x6027;</span></div><div class="line">    <span class="comment">// &#x8BE5;&#x65B9;&#x6CD5;&#xFF0C;&#x4F1A;&#x5C06;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x7684; url &#x8DEF;&#x5F84;&#x89E3;&#x6790;&#x4E3A;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x65B9;&#x4FBF;&#x6211;&#x4EEC;&#x53D6;&#x5404;&#x4E2A;&#x90E8;&#x5206;&#x7684;&#x6570;&#x636E;</span></div><div class="line">    <span class="keyword">const</span> urlObj = url.parse(req.url, <span class="literal">true</span>);</div><div class="line">    <span class="keyword">const</span> pathname = urlObj.pathname; <span class="comment">// &#x62FF;&#x5230;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#xFF0C;&#x4E0D;&#x5305;&#x542B;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;</span></div><div class="line">    <span class="keyword">const</span> queryObj = urlObj.query; <span class="comment">// &#x62FF;&#x5230;&#x901A;&#x8FC7; url.parse &#x65B9;&#x6CD5;&#x89E3;&#x6790;&#x51FA;&#x6765;&#x7684;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#xFF08;&#x5DF2;&#x7ECF;&#x81EA;&#x52A8;&#x8F6C;&#x4E3A;&#x5BF9;&#x8C61;&#x4E86;&#xFF09;</span></div><div class="line"></div><div class="line">    <span class="keyword">if</span> (pathname === <span class="string">&apos;/&apos;</span>) {</div><div class="line">      fs.readFile(<span class="string">&apos;./views/index.html&apos;</span>, (err, data) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">throw</span> err;</div><div class="line">        }</div><div class="line">        res.writeHead(<span class="number">200</span>, {</div><div class="line">          <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/html; charset=utf-8&apos;</span></div><div class="line">        });</div><div class="line">        res.end(data);</div><div class="line">      });</div><div class="line">    } <span class="keyword">else</span> <span class="keyword">if</span> (pathname.startsWith(<span class="string">&apos;/public/&apos;</span>) || pathname.startsWith(<span class="string">&apos;/uploads/&apos;</span>) || pathname.startsWith(<span class="string">&apos;/node_modules/&apos;</span>)) {</div><div class="line">      fs.readFile(<span class="string">`.<span class="subst">${pathname}</span>`</span>, (err, data) =&gt; {</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          res.writeHead(<span class="number">404</span>, <span class="string">&apos;Not Found&apos;</span>);</div><div class="line">          res.end();</div><div class="line">        }</div><div class="line">        res.writeHead(<span class="number">200</span>, {</div><div class="line">          <span class="string">&apos;Content-Type&apos;</span>: mime.lookup(pathname)</div><div class="line">        })</div><div class="line">        res.end(data);</div><div class="line">      });</div><div class="line">    }</div><div class="line">  })</div><div class="line">  .listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&apos;Server is running at port 3000.&apos;</span>);</div><div class="line">  });</div></pre></td></tr></table></figure>
<h2 id="&#x8DEF;&#x7531;&#xFF1A;&#x6839;&#x636E;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x505A;&#x5904;&#x7406;&#x3002;"><a href="#&#x8DEF;&#x7531;&#xFF1A;&#x6839;&#x636E;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x505A;&#x5904;&#x7406;&#x3002;" class="headerlink" title="&#x8DEF;&#x7531;&#xFF1A;&#x6839;&#x636E;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x505A;&#x5904;&#x7406;&#x3002;"></a>&#x8DEF;&#x7531;&#xFF1A;&#x6839;&#x636E;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x505A;&#x5904;&#x7406;&#x3002;</h2><p>app.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">&apos;http&apos;</span>);</div><div class="line"><span class="keyword">const</span> router = <span class="built_in">require</span>(<span class="string">&apos;./router&apos;</span>);</div><div class="line"></div><div class="line">http</div><div class="line">  .createServer()</div><div class="line">  .on(<span class="string">&apos;request&apos;</span>, (req, res) =&gt; {</div><div class="line">    router(req, res);</div><div class="line">  })</div><div class="line">  .listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&apos;Server is running at port 3000.&apos;</span>);</div><div class="line">  });</div></pre></td></tr></table></figure>
<p>&#x63D0;&#x53D6;&#x7684;&#x8DEF;&#x7531;&#x6A21;&#x5757;</p>
<p>router.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="keyword">const</span> url = <span class="built_in">require</span>(<span class="string">&apos;url&apos;</span>);</div><div class="line"><span class="keyword">const</span> mime = <span class="built_in">require</span>(<span class="string">&apos;mime&apos;</span>);</div><div class="line"><span class="built_in">module</span>.exports = funciton(req, res) {</div><div class="line">  <span class="keyword">const</span> urlObj = url.parse(req.url, <span class="literal">true</span>);</div><div class="line">  <span class="keyword">const</span> pathname = urlObj.pathname; </div><div class="line">  <span class="keyword">const</span> queryObj = urlObj.query;</div><div class="line"></div><div class="line">  <span class="comment">// &#x4E00;&#x4E2A;&#x8BF7;&#x6C42;&#x5BF9;&#x5E94;&#x4E86;&#x4E00;&#x4E2A;&#x5904;&#x7406;&#x6D41;&#x7A0B;&#x4EE3;&#x7801;</span></div><div class="line">  <span class="keyword">if</span> (pathname === <span class="string">&apos;/&apos;</span>) {</div><div class="line">    fs.readFile(<span class="string">&apos;./views/index.html&apos;</span>, (err, data) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">throw</span> err;</div><div class="line">      }</div><div class="line">      res.writeHead(<span class="number">200</span>, {</div><div class="line">        <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/html; charset=utf-8&apos;</span></div><div class="line">      });</div><div class="line">      res.end(data);</div><div class="line">    });</div><div class="line">  } <span class="keyword">else</span> <span class="keyword">if</span> (pathname.startsWith(<span class="string">&apos;/public/&apos;</span>) || pathname.startsWith(<span class="string">&apos;/uploads/&apos;</span>) || pathname.startsWith(<span class="string">&apos;/node_modules/&apos;</span>)) {</div><div class="line">    fs.readFile(<span class="string">`.<span class="subst">${pathname}</span>`</span>, (err, data) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        res.writeHead(<span class="number">404</span>, <span class="string">&apos;Not Found&apos;</span>);</div><div class="line">        res.end();</div><div class="line">      }</div><div class="line">      res.writeHead(<span class="number">200</span>, {</div><div class="line">        <span class="string">&apos;Content-Type&apos;</span>: mime.lookup(pathname)</div><div class="line">      })</div><div class="line">      res.end(data);</div><div class="line">    });</div><div class="line">  }</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="&#x518D;&#x6B21;&#x5C01;&#x88C5;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x7684;&#x4EE3;&#x7801;-&#x5904;&#x7406;&#x6A21;&#x5757;&#xFF0C;&#x66B4;&#x6F0F;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x65B9;&#x6CD5;"><a href="#&#x518D;&#x6B21;&#x5C01;&#x88C5;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x7684;&#x4EE3;&#x7801;-&#x5904;&#x7406;&#x6A21;&#x5757;&#xFF0C;&#x66B4;&#x6F0F;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x65B9;&#x6CD5;" class="headerlink" title="&#x518D;&#x6B21;&#x5C01;&#x88C5;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x7684;&#x4EE3;&#x7801;, &#x5904;&#x7406;&#x6A21;&#x5757;&#xFF0C;&#x66B4;&#x6F0F;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x65B9;&#x6CD5;"></a>&#x518D;&#x6B21;&#x5C01;&#x88C5;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x7684;&#x4EE3;&#x7801;, &#x5904;&#x7406;&#x6A21;&#x5757;&#xFF0C;&#x66B4;&#x6F0F;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x65B9;&#x6CD5;</h2><p>router.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="keyword">const</span> url = <span class="built_in">require</span>(<span class="string">&apos;url&apos;</span>);</div><div class="line"><span class="keyword">const</span> mime = <span class="built_in">require</span>(<span class="string">&apos;mime&apos;</span>);</div><div class="line"><span class="keyword">const</span> handler = <span class="built_in">require</span>(<span class="string">&apos;./handler&apos;</span>);</div><div class="line"></div><div class="line"><span class="built_in">module</span>.exports = funciton(req, res) {</div><div class="line">  <span class="keyword">const</span> urlObj = url.parse(req.url, <span class="literal">true</span>);</div><div class="line">  <span class="keyword">const</span> pathname = urlObj.pathname; </div><div class="line">  <span class="keyword">const</span> queryObj = urlObj.query;</div><div class="line"></div><div class="line">  <span class="comment">// &#x4E00;&#x4E2A;&#x8BF7;&#x6C42;&#x5BF9;&#x5E94;&#x4E86;&#x4E00;&#x4E2A;&#x5904;&#x7406;&#x6D41;&#x7A0B;&#x4EE3;&#x7801;</span></div><div class="line">  <span class="keyword">if</span> (pathname === <span class="string">&apos;/&apos;</span>) {</div><div class="line">    handler.showIndex(req, res);</div><div class="line">  } <span class="keyword">else</span> <span class="keyword">if</span> (pathname.startsWith(<span class="string">&apos;/public/&apos;</span>) || pathname.startsWith(<span class="string">&apos;/uploads/&apos;</span>) || pathname.startsWith(<span class="string">&apos;/node_modules/&apos;</span>)) {</div><div class="line">    handler.showPublic(req, res);</div><div class="line">  }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>hander.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x6E32;&#x67D3;&#x9996;&#x9875;</span></div><div class="line">exports.showIndex = <span class="function">(<span class="params">req, res</span>) =&gt;</span> {</div><div class="line">  fs.readFile(<span class="string">&apos;./views/index.html&apos;</span>, (err, data) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">throw</span> err;</div><div class="line">    }</div><div class="line">    res.writeHead(<span class="number">200</span>, {</div><div class="line">      <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/html; charset=utf-8&apos;</span></div><div class="line">    });</div><div class="line">    res.end(data);</div><div class="line">  });</div><div class="line">};</div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x6DFB;&#x52A0;&#x76F8;&#x518C;</span></div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x6E32;&#x67D3;&#x76F8;&#x518C;&#x9875;&#x9762;</span></div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x4E0A;&#x4F20;&#x7167;&#x7247;&#x8BF7;&#x6C42;</span></div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x8BF7;&#x6C42;</span></div><div class="line">exports.showPublic = <span class="function">(<span class="params">req, res</span>) =&gt;</span> {</div><div class="line">  fs.readFile(<span class="string">`.<span class="subst">${req.url}</span>`</span>, (err, data) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      res.writeHead(<span class="number">404</span>, <span class="string">&apos;Not Found&apos;</span>);</div><div class="line">      res.end();</div><div class="line">    }</div><div class="line">    res.writeHead(<span class="number">200</span>, {</div><div class="line">      <span class="string">&apos;Content-Type&apos;</span>: mime.lookup(req.url)</div><div class="line">    })</div><div class="line">    res.end(data);</div><div class="line">  });</div><div class="line">};</div></pre></td></tr></table></figure>
<p>&#x6A21;&#x5757;&#x6E05;&#x6670;-&#x804C;&#x8D23;&#x5355;&#x4E00;</p>
<p>&#x5BA2;&#x6237;&#x7AEF; -&gt; app.js (1.&#x542F;&#x52A8;&#x670D;&#x52A1;&#x5668;; 2. &#x5C06;&#x8BF7;&#x6C42;&#x4F20;&#x9012;&#x5230; router &#x6A21;&#x5757;&#x4E2D;) -&gt; router.js &#x8DEF;&#x7531;&#x6A21;&#x5757; (&#x6839;&#x636E;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x8C03;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x5904;&#x7406;&#x51FD;&#x6570;) -&gt; handler.js &#x8BF7;&#x6C42;&#x5904;&#x7406;&#x51FD;&#x6570;</p>
<h2 id="&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><a href="#&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;" class="headerlink" title="&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;"></a>&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;</h2><p>template.js &#xFF08;&#x5148;&#x770B;&#x6709;&#x5173; underscore &#x6A21;&#x677F;&#x7684;&#x4F7F;&#x7528;&#x7684;&#x5C0F;&#x4F8B;&#x5B50;&#xFF09;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> _ = <span class="built_in">require</span>(<span class="string">&apos;underscore&apos;</span>);</div><div class="line"><span class="comment">// 1. &#x5C06;&#x6A21;&#x677F;&#x5B57;&#x7B26;&#x4E32;&#x4F20;&#x9012;&#x7ED9; _.template &#x65B9;&#x6CD5;</span></div><div class="line"><span class="comment">//    &#x8BE5;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x7F16;&#x8BD1;&#x51FD;&#x6570;</span></div><div class="line"><span class="comment">// 2. &#x8C03;&#x7528;&#x7F16;&#x8BD1;&#x51FD;&#x6570;&#xFF0C;&#x4F20;&#x5165;&#x8981;&#x6CE8;&#x5165;&#x7684;&#x6570;&#x636E;&#x5BF9;&#x8C61;</span></div><div class="line"><span class="keyword">const</span> complied = _.template(<span class="string">`hello &lt;%= name %&gt;</span></div><div class="line">&lt;ul&gt; </div><div class="line">  &lt;% fruits.forEach(function (fruit)){ %&gt;</div><div class="line">    &lt;li&gt;&lt;%= fruit %&gt;&lt;li&gt;</div><div class="line">  &lt;% }) %&gt;</div><div class="line">&lt;/ul&gt;</div><div class="line">`);</div><div class="line"><span class="built_in">console</span>.log(complied({</div><div class="line">  <span class="attr">name</span>: <span class="string">&quot;Hiraku&quot;</span>,</div><div class="line">  <span class="attr">fruits</span>: [</div><div class="line">    <span class="string">&apos;&#x6817;&#x5B50;&apos;</span>,</div><div class="line">    <span class="string">&apos;&#x82F9;&#x679C;&apos;</span>,</div><div class="line">    <span class="string">&apos;&#x6A59;&#x5B50;&apos;</span></div><div class="line">  ]</div><div class="line">}));</div></pre></td></tr></table></figure>
<p>&#x73B0;&#x5728;&#x5C06;&#x6A21;&#x677F;&#x5B57;&#x7B26;&#x4E32;&#x5229;&#x7528;&#x8BFB;&#x6587;&#x4EF6;&#x7684;&#x65B9;&#x5F0F;&#x8BFB;&#x51FA;&#x6765;&#x3002;(tpt.html)</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">ul</span>&gt;</span> </div><div class="line">    <span class="tag">&lt;<span class="name">%</span> <span class="attr">fruits.forEach</span>(<span class="attr">function</span> (<span class="attr">fruit</span>)){ %&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">li</span>&gt;</span><span class="tag">&lt;<span class="name">%=</span> <span class="attr">fruit</span> %&gt;</span><span class="tag">&lt;<span class="name">li</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">%</span> }) %&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<p>&#x73B0;&#x5728;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5199; template.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> _ = <span class="built_in">require</span>(<span class="string">&apos;underscore&apos;</span>);</div><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line">fs.readFile(<span class="string">&apos;./tpl.html&apos;</span>, <span class="string">&apos;utf8&apos;</span>, (err, data) =&gt; {</div><div class="line">  <span class="keyword">if</span> (err) {</div><div class="line">    <span class="keyword">throw</span> err;</div><div class="line">  }</div><div class="line">  <span class="keyword">const</span> complied = _.template(data);</div><div class="line">  <span class="built_in">console</span>.log(complied({</div><div class="line">    <span class="attr">fruits</span>: [</div><div class="line">      <span class="string">&apos;&#x6817;&#x5B50;&apos;</span>,</div><div class="line">      <span class="string">&apos;&#x82F9;&#x679C;&apos;</span>,</div><div class="line">      <span class="string">&apos;&#x6A59;&#x5B50;&apos;</span></div><div class="line">    ]</div><div class="line">  }));</div><div class="line">});</div></pre></td></tr></table></figure>
<h2 id="&#x5B8C;&#x6210;&#x9996;&#x9875;&#x6E32;&#x67D3;&#x76F8;&#x518C;&#x8868;&#x529F;&#x80FD;"><a href="#&#x5B8C;&#x6210;&#x9996;&#x9875;&#x6E32;&#x67D3;&#x76F8;&#x518C;&#x8868;&#x529F;&#x80FD;" class="headerlink" title="&#x5B8C;&#x6210;&#x9996;&#x9875;&#x6E32;&#x67D3;&#x76F8;&#x518C;&#x8868;&#x529F;&#x80FD;"></a>&#x5B8C;&#x6210;&#x9996;&#x9875;&#x6E32;&#x67D3;&#x76F8;&#x518C;&#x8868;&#x529F;&#x80FD;</h2><p>app.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">&apos;http&apos;</span>);</div><div class="line"><span class="keyword">const</span> router = <span class="built_in">require</span>(<span class="string">&apos;./router&apos;</span>);</div><div class="line"></div><div class="line">http</div><div class="line">  .createServer()</div><div class="line">  .on(<span class="string">&apos;request&apos;</span>, (req, res) =&gt; {</div><div class="line">    router(req, res);</div><div class="line">  })</div><div class="line">  .listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">&apos;Server is running at port 3000.&apos;</span>);</div><div class="line">  });</div></pre></td></tr></table></figure>
<p>hander.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&apos;fs&apos;</span>);</div><div class="line"><span class="keyword">const</span> _ = <span class="built_in">require</span>(<span class="string">&apos;underscore&apos;</span>);</div><div class="line"><span class="keyword">const</span> url = <span class="built_in">require</span>(<span class="string">&apos;mine&apos;</span>)</div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x6E32;&#x67D3;&#x9996;&#x9875;</span></div><div class="line">exports.showIndex = <span class="function">(<span class="params">req, res</span>) =&gt;</span> {</div><div class="line">  fs.readdir(<span class="string">&apos;./uploads&apos;</span>, (err, files) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">throw</span> err;</div><div class="line">    }</div><div class="line">    fs.readFile(<span class="string">&apos;./views/index.html&apos;</span>, <span class="string">&apos;utf8&apos;</span>, (err, data) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">throw</span> err;</div><div class="line">      }</div><div class="line">      <span class="keyword">const</span> result = _.template(data)(</div><div class="line">        albumNames: files</div><div class="line">      );</div><div class="line">      res.writeHead(<span class="number">200</span>, {</div><div class="line">        <span class="string">&apos;Content-Type&apos;</span>: <span class="string">&apos;text/html; charset=utf-8&apos;</span></div><div class="line">      });</div><div class="line">      res.end(result);</div><div class="line">    });</div><div class="line">  });</div><div class="line">};</div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x6DFB;&#x52A0;&#x76F8;&#x518C;</span></div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x6E32;&#x67D3;&#x76F8;&#x518C;&#x9875;&#x9762;</span></div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x4E0A;&#x4F20;&#x7167;&#x7247;&#x8BF7;&#x6C42;</span></div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x8BF7;&#x6C42;</span></div><div class="line">exports.showPublic = <span class="function">(<span class="params">req, res</span>) =&gt;</span> {</div><div class="line">  fs.readFile(<span class="string">`.<span class="subst">${req.url}</span>`</span>, (err, data) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      res.writeHead(<span class="number">404</span>, <span class="string">&apos;Not Found&apos;</span>);</div><div class="line">      res.end();</div><div class="line">    }</div><div class="line">    res.writeHead(<span class="number">200</span>, {</div><div class="line">      <span class="string">&apos;Content-Type&apos;</span>: mime.lookup(req.url)</div><div class="line">    })</div><div class="line">    res.end(data);</div><div class="line">  });</div><div class="line">};</div></pre></td></tr></table></figure>
<p>router.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="keyword">const</span> url = <span class="built_in">require</span>(<span class="string">&apos;url&apos;</span>);</div><div class="line"><span class="keyword">const</span> handler = <span class="built_in">require</span>(<span class="string">&apos;./handler&apos;</span>);</div><div class="line"><span class="built_in">module</span>.exports = funciton(req, res) {</div><div class="line">  <span class="keyword">const</span> urlObj = url.parse(req.url, <span class="literal">true</span>);</div><div class="line">  <span class="keyword">const</span> pathname = urlObj.pathname; </div><div class="line">  <span class="keyword">const</span> queryObj = urlObj.query;</div><div class="line"></div><div class="line">  <span class="comment">// &#x4E00;&#x4E2A;&#x8BF7;&#x6C42;&#x5BF9;&#x5E94;&#x4E86;&#x4E00;&#x4E2A;&#x5904;&#x7406;&#x6D41;&#x7A0B;&#x4EE3;&#x7801;</span></div><div class="line">  <span class="keyword">if</span> (pathname === <span class="string">&apos;/&apos;</span>) {</div><div class="line">    handler.showIndex(req, res);</div><div class="line">  } <span class="keyword">else</span> <span class="keyword">if</span> (pathname.startsWith(<span class="string">&apos;/public/&apos;</span>) || pathname.startsWith(<span class="string">&apos;/uploads/&apos;</span>) || pathname.startsWith(<span class="string">&apos;/node_modules/&apos;</span>)) {</div><div class="line">    handler.showPublic(req, res);</div><div class="line">  }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>./views/index.html</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;!doctype html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>&#x6211;&#x7684;&#x76F8;&#x518C;<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;node_modules/bootstrap/dist/css/bootstrap.css&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;public/css/main.css&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container-fluid&quot;</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">&quot;navbar navbar-default&quot;</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container-fluid&quot;</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;navbar-header&quot;</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;button&quot;</span> <span class="attr">class</span>=<span class="string">&quot;navbar-toggle collapsed&quot;</span> <span class="attr">data-toggle</span>=<span class="string">&quot;collapse&quot;</span> <span class="attr">data-target</span>=<span class="string">&quot;#bs-example-navbar-collapse-1&quot;</span> <span class="attr">aria-expanded</span>=<span class="string">&quot;false&quot;</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;sr-only&quot;</span>&gt;</span>Toggle navigation<span class="tag">&lt;/<span class="name">span</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;icon-bar&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;icon-bar&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;icon-bar&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">button</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">&quot;navbar-brand&quot;</span> <span class="attr">href</span>=<span class="string">&quot;/&quot;</span>&gt;</span>&#x6211;&#x7684;&#x76F8;&#x518C;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;collapse navbar-collapse&quot;</span> <span class="attr">id</span>=<span class="string">&quot;bs-example-navbar-collapse-1&quot;</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;nav navbar-nav&quot;</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;active&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;/&quot;</span>&gt;</span>&#x9996;&#x9875; <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;sr-only&quot;</span>&gt;</span>(current)<span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;nav navbar-nav navbar-right&quot;</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">li</span>&gt;</span></div><div class="line">              <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;&quot;</span> <span class="attr">data-toggle</span>=<span class="string">&quot;modal&quot;</span> <span class="attr">data-target</span>=<span class="string">&quot;#exampleModal&quot;</span> <span class="attr">data-whatever</span>=<span class="string">&quot;@mdo&quot;</span>&gt;</span>&#x65B0;&#x5EFA;&#x76F8;&#x518C;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">li</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">nav</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></div><div class="line">    <span class="comment">&lt;!-- &#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE; --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;row&quot;</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">%</span> <span class="attr">albumNames.forEach</span>(<span class="attr">function</span>(<span class="attr">albumName</span>){ %&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-xs-6 col-md-3&quot;</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span> <span class="attr">class</span>=<span class="string">&quot;thumbnail&quot;</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;public/img/icon.png&quot;</span> <span class="attr">alt</span>=<span class="string">&quot;&quot;</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;caption&quot;</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">h3</span>&gt;</span><span class="tag">&lt;<span class="name">%=</span> <span class="attr">albumName</span> %&gt;</span><span class="tag">&lt;/<span class="name">h3</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">%</span> }) %&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;modal fade&quot;</span> <span class="attr">id</span>=<span class="string">&quot;exampleModal&quot;</span> <span class="attr">tabindex</span>=<span class="string">&quot;-1&quot;</span> <span class="attr">role</span>=<span class="string">&quot;dialog&quot;</span> <span class="attr">aria-labelledby</span>=<span class="string">&quot;exampleModalLabel&quot;</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;modal-dialog&quot;</span> <span class="attr">role</span>=<span class="string">&quot;document&quot;</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;modal-content&quot;</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">&quot;/&quot;</span> <span class="attr">method</span>=<span class="string">&quot;post&quot;</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;modal-header&quot;</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;button&quot;</span> <span class="attr">class</span>=<span class="string">&quot;close&quot;</span> <span class="attr">data-dismiss</span>=<span class="string">&quot;modal&quot;</span> <span class="attr">aria-label</span>=<span class="string">&quot;Close&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">span</span> <span class="attr">aria-hidden</span>=<span class="string">&quot;true&quot;</span>&gt;</span>&amp;times;<span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">button</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">h4</span> <span class="attr">class</span>=<span class="string">&quot;modal-title&quot;</span> <span class="attr">id</span>=<span class="string">&quot;exampleModalLabel&quot;</span>&gt;</span>&#x65B0;&#x5EFA;&#x76F8;&#x518C;<span class="tag">&lt;/<span class="name">h4</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;modal-body&quot;</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;form-group&quot;</span>&gt;</span></div><div class="line">              <span class="tag">&lt;<span class="name">label</span> <span class="attr">for</span>=<span class="string">&quot;recipient-name&quot;</span> <span class="attr">class</span>=<span class="string">&quot;control-label&quot;</span>&gt;</span>&#x76F8;&#x518C;&#x540D;&#x79F0;:<span class="tag">&lt;/<span class="name">label</span>&gt;</span></div><div class="line">              <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">name</span>=<span class="string">&quot;albumName&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x76F8;&#x518C;&#x540D;&#x79F0;&quot;</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;modal-footer&quot;</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;button&quot;</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-default&quot;</span> <span class="attr">data-dismiss</span>=<span class="string">&quot;modal&quot;</span>&gt;</span>&#x5173;&#x95ED;<span class="tag">&lt;/<span class="name">button</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-success&quot;</span>&gt;</span>&#x70B9;&#x51FB;&#x6DFB;&#x52A0;<span class="tag">&lt;/<span class="name">button</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">form</span>&gt;</span></div><div class="line">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;node_modules/jquery/dist/jquery.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;node_modules/bootstrap/dist/js/bootstrap.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<p><img src="/2016/10/14/s008-node-&#x76F8;&#x518C;&#x5C0F;&#x9879;&#x76EE;&#xFF08;&#x4E0A;&#xFF09;/&#x8BF7;&#x6C42;&#x5904;&#x7406;&#x6A21;&#x5757;&#x8C03;&#x7528;&#x6D41;&#x7A0B;&#x56FE;.png" alt="&#x8BF7;&#x6C42;&#x5904;&#x7406;&#x6A21;&#x5757;&#x8C03;&#x7528;&#x6D41;&#x7A0B;&#x56FE;"></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>感谢您的支持！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>打 赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="http://ofy7k3v4s.bkt.clouddn.com/wcpayCode.jpg" alt="Hongqin Ma WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="http://ofy7k3v4s.bkt.clouddn.com/apayCode.jpg" alt="Hongqin Ma Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Node-js/" rel="tag">#Node.js</a>
          
            <a href="/tags/小项目/" rel="tag">#小项目</a>
          
            <a href="/tags/mine/" rel="tag">#mine</a>
          
            <a href="/tags/Underscore/" rel="tag">#Underscore</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/10/09/s007-node-http-网站/" rel="next" title="Node-http-网站">
                <i class="fa fa-chevron-left"></i> Node-http-网站
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/10/18/s009-node-相册小项目（中）/" rel="prev" title="Node-相册小项目（中）">
                Node-相册小项目（中） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2016/10/14/s008-node-相册小项目（上）/"
     data-title="Node-相册小项目（上）"
     data-content=""
     data-url="https://hongqinma.github.io/2016/10/14/s008-node-相册小项目（上）/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/10/14/s008-node-相册小项目（上）/"
           data-title="Node-相册小项目（上）" data-url="https://hongqinma.github.io/2016/10/14/s008-node-相册小项目（上）/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/photo.jpg"
               alt="Hongqin Ma" />
          <p class="site-author-name" itemprop="name">Hongqin Ma</p>
          <p class="site-description motion-element" itemprop="description">Spell at all costs， to rush you in the future. </p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">68</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">16</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">57</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/HongqinMa" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/HongqinMa" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.facebook.com/profile.php?id=100006581058735" target="_blank" title="Facebook">
                  
                    <i class="fa fa-fw fa-facebook"></i>
                  
                  Facebook
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/HirakuMa" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-share-alt"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/rubylouvre/" title="司徒正美" target="_blank">司徒正美</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/sanshi/" title="三生石上" target="_blank">三生石上</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/TomXu/" title="汤姆大叔" target="_blank">汤姆大叔</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://gold.xitu.io/" title="掘金" target="_blank">掘金</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://segmentfault.com/" title="segmentfault" target="_blank">segmentfault</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.jianshu.com/users/1cbb94e5ed4d/latest_articles" title="简书" target="_blank">简书</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://home.cnblogs.com/" title="博客园" target="_blank">博客园</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://www.awesomes.cn/" title="Awesomes" target="_blank">Awesomes</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#项目初始化"><span class="nav-number">1.</span> <span class="nav-text">项目初始化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#划分哪些资源公共开放"><span class="nav-number">2.</span> <span class="nav-text">划分哪些资源公共开放</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#设计路由"><span class="nav-number">3.</span> <span class="nav-text">设计路由</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#处理相册静态资源"><span class="nav-number">4.</span> <span class="nav-text">处理相册静态资源</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#路由：根据不同的请求路径做处理。"><span class="nav-number">5.</span> <span class="nav-text">路由：根据不同的请求路径做处理。</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#再次封装处理请求的代码-处理模块，暴漏一系列的方法"><span class="nav-number">6.</span> <span class="nav-text">再次封装处理请求的代码, 处理模块，暴漏一系列的方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用模板引擎"><span class="nav-number">7.</span> <span class="nav-text">使用模板引擎</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#完成首页渲染相册表功能"><span class="nav-number">8.</span> <span class="nav-text">完成首页渲染相册表功能</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Hongqin Ma</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span id="busuanzi_container_site_pv">
    &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;Page Views: <span id="busuanzi_value_site_pv"></span> 
</span>

        

<div class="busuanzi-count">

  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv"><i class="fa fa-user"></i><span class="busuanzi-value" id="busuanzi_value_site_uv"></span></span>
  

  
    <span class="site-pv"><i class="fa fa-eye"></i><span class="busuanzi-value" id="busuanzi_value_site_pv"></span></span>
  
  
</div>



        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"Hiraku"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
<script>
function showTime(Counter) {
  var query = new AV.Query(Counter);
  $(".leancloud_visitors").each(function() {
    var url = $(this).attr("id").trim();
    query.equalTo("url", url);
    query.find({
      success: function(results) {
        if (results.length == 0) {
          var content = '0 ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
          return;
        }
        for (var i = 0; i < results.length; i++) {
          var object = results[i];
          var content = object.get('time') + ' ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
        }
      },
      error: function(object, error) {
        console.log("Error: " + error.code + " " + error.message);
      }
    });

  });
}

function addCount(Counter) {
  var Counter = AV.Object.extend("Counter");
  url = $(".leancloud_visitors").attr('id').trim();
  title = $(".leancloud_visitors").attr('data-flag-title').trim();
  var query = new AV.Query(Counter);
  query.equalTo("url", url);
  query.find({
    success: function(results) {
      if (results.length > 0) {
        var counter = results[0];
        counter.fetchWhenSave(true);
        counter.increment("time");
        counter.save(null, {
          success: function(counter) {
            var content =  counter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(counter, error) {
            console.log('Failed to save Visitor num, with error message: ' + error.message);
          }
        });
      } else {
        var newcounter = new Counter();
        newcounter.set("title", title);
        newcounter.set("url", url);
        newcounter.set("time", 1);
        newcounter.save(null, {
          success: function(newcounter) {
              console.log("newcounter.get('time')="+newcounter.get('time'));
            var content = newcounter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(newcounter, error) {
            console.log('Failed to create');
          }
        });
      }
    },
    error: function(error) {
      console.log('Error:' + error.code + " " + error.message);
    }
  });
}
$(function() {
  var Counter = AV.Object.extend("Counter");
  if ($('.leancloud_visitors').length == 1) {
    addCount(Counter);
  } else if ($('.post-title-link').length > 1) {
    showTime(Counter);
  }
}); 
</script>

  
</body>
</html>
